//放弃 赠送  消耗  销毁  
//giveupCount   largessCount  consumeCount   destroyCount   
//转关  留购  回运  其他
//transportCount   buyCount   returnCount   otherCount
//罚没   遗失
//punishCount loseCount
//报关单号 通关单号
//declareNo   passageNo
//核销选择  差额   企业备注  审核意见
//clearOption  balance   entOpinion   auditOpinion

//ciq 审核确认核销
function ciqAuditGoodsClear(goodsId) {
	document.getElementById("clearFlag"+goodsId).value='5';//批准
	if(!_validateClearOption(goodsId)){
		return false;
	}
	var balanceCount=_getGoodsClearBalanceCount(goodsId);
	if(balanceCount!=0){
		if(window.confirm("进境货物总量与核销总量不等，您确定要批准此核销吗？")){
			return true;
		}else{
			return false;
		}
	}else{
		if(!window.confirm("您确定要批准此核销吗？")){
			return false;
		}
	}
	return true;
}

//ciq 退回核销
//return boolean
function ciqUntreadClearGoods(goodsId){
	document.getElementById("clearFlag"+goodsId).value='3';//退回
	_getGoodsClearBalanceCount(goodsId);
	var auditOpinion=document.getElementById("auditOpinion"+goodsId).value.trim();
	if(auditOpinion.length==0){
		alert("如果退回企业核销申请，请填写审核意见");
		return false;
	}
	if(!window.confirm("您确定要退回此核销申请吗？")){
		return false;
	}
	return true;
}

//ent 提交核销
//return boolean
function entSubmitGoodsClear(forceClear,goodsId,goodsName) {
	if(!_validateClearOption(goodsId)){
		return false;
	}
	var balanceCount=_getGoodsClearBalanceCount(goodsId);
	if(0!=balanceCount){
		if(!_validateForceClear(forceClear,goodsId,balanceCount)){
			return false;
		}
	}
	if(!_validatePassageNoClear(goodsId)){
		return false;
	}
	if(!window.confirm("你是否确认提交\""+goodsName+"\"的核销结果")){
		return false;
	}
	
	document.getElementById("clearFlag"+goodsId).value='4';//提交
	return true;
}

//ent 暂存
//return boolean
function entSaveGoodsClear(goodsId){
	if(!_validateClearOption(goodsId)){
		return false;
	}
	_getGoodsClearBalanceCount(goodsId);
	document.getElementById("clearFlag"+goodsId).value='2';//暂存
	return true;
}

//核销选择检查
//return boolean
function _validateClearOption(goodsId){
	var sum=_getGoodsSumCount(goodsId);
	if(sum<0){
		alert("您必须选择报检货物数量或重量中有意义的一项");	
		return false;
	}
	return true;
}

//强制核销检查
//return boolean
function _validateForceClear(forceClear,goodsId,balanceCount){
	if(!forceClear){
		alert("进境货物总量与核销总量相差\""+Math.abs(balanceCount)+"\"，如果必须核销请选择\"强制核销\"，否则请检查核销数值!");
		return false;
	}else{
		var entOpinion=document.getElementById("entOpinion"+goodsId).value.trim();
		if(0==entOpinion.length){
			alert("进境货物总量与核销总量不等，请在\"企业备注\"中填写原因");
			return false;
		}
	}
	return true;
}

//通关单检查
//return boolean
function _validatePassageNoClear(goodsId){
	var inspMonitorCond=document.getElementById("inspMonitorCond"+goodsId).innerText.trim();
	var mn=inspMonitorCond;
	var isBuy=false;//留购监管
	var isReturn=false;//回运监管
	if(0!=mn.length){
		if(mn.indexOf("P")!=-1||mn.indexOf("R")!=-1||mn.indexOf("M")!=-1||mn.indexOf("L")!=-1){
			isBuy=true;//有留购监管
 		}
 		if(mn.indexOf("Q")!=-1||mn.indexOf("S")!=-1||mn.indexOf("N")!=-1){
 			isReturn=true;//有回运监管
 		}
 		
 		var buyCount=document.getElementById("buyCount"+goodsId).value.trim();
 		var returnCount=document.getElementById("returnCount"+goodsId).value.trim();
 		var declareNo=document.getElementById("declareNo"+goodsId).value.trim();
 		var passageNo=document.getElementById("passageNo"+goodsId).value.trim();
		
		buyCount=_safeParseFloat(buyCount);
		returnCount=_safeParseFloat(returnCount);
 		if(isBuy&&0!=buyCount&&0==passageNo.length){
 			alert("当货物'监管类别'是P/R/M/L类别,且'留购'数量不为0时,必须填写'通关单号'");
 			return false;
 		}
 		if(isReturn&&0!=returnCount&&0==passageNo.length){
 			alert("当展品'监管类别'是Q/S/N类别,且'回运'数量不为0时,必须填写'通关单号'");
 			return false;
 		}
 		if(0!=passageNo.length&&0==returnCount&&0==buyCount){
 			alert("您已填写'通关单号',请填写'留购'或'回运'数量");
 			return false;
 		}
 		if(0!=returnCount.length&&0!=buyCount&&0==declareNo){
 			alert("当展品'留购'或'回运'数量不为0时,必须填写'报关单号'");
 			return false;
 		}
	}
	return true;
}

//验证核销结果差额
//return float balanceCount 
function _getGoodsClearBalanceCount(goodsId) {
	var sum=_getGoodsSumCount(goodsId);
	
	var giveupCount   = _safeParseFloat(document.getElementById("giveupCount"+goodsId).value   );
	var largessCount  = _safeParseFloat(document.getElementById("largessCount"+goodsId).value  );
	var consumeCount  = _safeParseFloat(document.getElementById("consumeCount"+goodsId).value  );
	var destroyCount  = _safeParseFloat(document.getElementById("destroyCount"+goodsId).value  );
	var transportCount= _safeParseFloat(document.getElementById("transportCount"+goodsId).value);
	var buyCount      = _safeParseFloat(document.getElementById("buyCount"+goodsId).value      );
	var returnCount   = _safeParseFloat(document.getElementById("returnCount"+goodsId).value   );
	var otherCount    = _safeParseFloat(document.getElementById("otherCount"+goodsId).value    );
	var punishCount    = _safeParseFloat(document.getElementById("punishCount"+goodsId).value    );
	var loseCount    = _safeParseFloat(document.getElementById("loseCount"+goodsId).value    );
	
	var clearCount=giveupCount + largessCount + consumeCount + destroyCount + transportCount 
				   + buyCount + returnCount + otherCount + punishCount + loseCount;
	//差额
	var balanceCount=sum-clearCount;
	if(Math.abs(balanceCount)<0.01){
		balanceCount=0;
	}else{
		balanceCount=roundFloat(balanceCount,3);
	}
	var balanceSpan=document.getElementById("balanceSpan"+goodsId);
	var balance=document.getElementById("balance"+goodsId);
	balanceSpan.innerText=balanceCount;
	balance.value=balanceCount;
	return balanceCount;
}

//return float
function _safeParseFloat(str){
	var f=parseFloat(str)
	if(isNaN(f)){
		return 0;
	}else{
		return f;
	}
}

//根据核销选择返回货物报检数/重量
//return float
function _getGoodsSumCount(goodsId){
	var clearOption=document.getElementById("clearOption"+goodsId);
	var sum;
	if(clearOption.selectedIndex==1){
		sum=document.getElementById("qty"+goodsId).value;
	}else if(clearOption.selectedIndex==2){
		sum=document.getElementById("weight"+goodsId).value;
	}
	
	if(null==sum||0==sum.length){
		sum=-1;
	}else{
		sum=_safeParseFloat(sum);
	}
	return sum;
}

//打开通关单详细信息
function openReleaseDeclWin(releaseNo){
	var url="ReleaseDeclView.html?releaseDeclNo="+releaseNo;
	openModalDialog(url,800,500);
}

